function loadQueryUpdateTable() {
  $("#queryUpdateDetailGridTable").jqGrid({url:"update_detail_log.action?BATCH_ID="+"", 
                    datatype:"json", 
                    height:500, 
                    width:1000, 
                    colNames:["工单号","批次号", "图片", "宝贝", "更换结果编码", "更换情况", "失败原因", "主宝贝编码"], 
                    colModel:[{name:"TRADE_ID", index:"TRADE_ID", hidden: true}, 
                              {name:"BATCH_ID", index:"BATCH_ID", hidden: true},
                              {name:"IMG", index:"IMG", formatter:formateadorLink1, sortable:false,align:"center", width:"40px"}, 
                              {name:"TITLE", index:"TITLE", formatter:formateadorLink2, align:"center", width:"120"}, 
                              {name:"TRADE_STATE", index:"TRADE_STATE", hidden: true},
                              {name:"TRADE_STATE_DESC", index:"TRADE_STATE_DESC", align:"center", width:"120"},
                              {name:"EXP_DESC", index:"EXP_DESC", align:"center", width:"200"},
                              {name:"IID", index:"IID", hidden: true}], 
                      viewrecords:true, 
                      rowNum:6, 
                      rowList:[6, 10, 20], 
                      multiselectWidth:50, 
                      loadComplete:function () {
                      setImageCellHeight("queryUpdateDetailGridTable");
                    }, 
                    onSelectRow:function (rowid) {
                    }, 
                    onSelectAll:function (ids, status) {
                    }, 
                    postData:{
                      'pager.totalCount':function(){return getTotalCount($("#queryUpdateDetailGridTable"));}
                    },  
                    jsonReader:{root:"dataset", 
                          page:"pager.curPageNo", 
                          total:"pager.totalPages", 
                          records:"pager.totalCount", 
                          repeatitems:false
                    }, 
                    prmNames:{rows:"pager.pageSize", 
                          page:"pager.curPageNo", 
                          sort:"pager.orderBy", 
                          order:"pager.order"
                    }, 
                    pager:"#queryUpdateDetailGridPager", 
                    caption:"本次自动更新详情"
  });
  $("#queryUpdateGridTable").jqGrid({url:"update_log.action", 
										datatype:"json", 
										height:200, 
										width:1000, 
										colNames:["批次号", "给哪些宝贝更换", "更换时间", "显示位置", /*"静态推广宝贝", "动态推广宝贝", "更换频率",*/ "更换结果", "重新更换",  "更换结果编码", "配置编码", "给哪些宝贝安装编码"], 
										colModel:[  {name:"BATCH_ID", index:"BATCH_ID",hidden:true},
													{name:"INSTALL_SETTING_DESC", index:"INSTALL_SETTING_DESC", align:"center", width:"80"},
													{name:"PLAN_EXEC_TIME", index:"PLAN_EXEC_TIME", align:"center", width:"70"},
													{name:"INSTALL_PLACE_DESC", index:"INSTALL_PLACE_DESC", align:"center", width:"80"},
													//{name:"STATIC_NUM_DESC", index:"STATIC_NUM_DESC", align:"center", width:"70"},
													//{name:"INTELL_SETTING_DESC", index:"INTELL_SETTING_DESC", align:"center", width:"120"},
													//{name:"FREQUENCY_DESC", index:"FREQUENCY_DESC", align:"center", width:"60"},
													{name:"TRADE_STATE_DESC", index:"TRADE_STATE_DESC", width:"70"},
													{name:"RE_EXECUTE", index:"RE_EXECUTE", formatter:reUpdateButton, sortable:false, align:"center", width:"70px"},
													{name:"TRADE_STATE", index:"TRADE_STATE", width:"60",hidden:true},
													{name:"PRO_ID", index:"PRO_ID", hidden:true},
													{name:"INSTALL_SETTING", index:"INSTALL_SETTING", width:"60",hidden:true}] , 
									  	viewrecords:true, 
									  	rowNum:5, 
									  	rowList:[5, 10], 
									  	loadComplete:function () {
                                            setImageCellHeight("queryUpdateGridTable",35);
                                            $("#queryUpdateGridTable").setSelection(1);
                                            var ret = $("#queryUpdateGridTable").jqGrid("getRowData", "1");
                                            $("#queryUpdateDetailGridTable").jqGrid("setGridParam", {
                                              url:"update_detail_log.action?BATCH_ID="+ret.BATCH_ID,
                                              page: 1
                                            }).trigger("reloadGrid");   
                                            //setUpdateGridTimeout();                   
										}, 
										onSelectRow:function (rowid) {
											if (rowid === null) {
												return;
											}else{
												var ret = $("#queryUpdateGridTable").jqGrid("getRowData", rowid);
												jQuery("#queryUpdateDetailGridTable").jqGrid("setGridParam",{url:"update_detail_log.action?BATCH_ID="+ret.BATCH_ID, page: 1}).trigger("reloadGrid");
											}
										}, 
										onSelectAll:function (ids, status) {
										}, 
										jsonReader:{root:"dataset", 
													page:"pager.curPageNo", 
													total:"pager.totalPages", 
													records:"pager.totalCount", 
													repeatitems:false
										}, 
										prmNames:{rows:"pager.pageSize", 
												  page:"pager.curPageNo", 
												  sort:"pager.orderBy", 
												  order:"pager.order"
										}, 
										pager:"#queryUpdateGridPager", 
										caption:"最近20次自动更新记录",
										subGrid: true,
										subGridRowExpanded: function(subgrid_id, row_id) {
											var ret = $("#queryUpdateGridTable").jqGrid("getRowData", row_id);
											if(ret.INSTALL_SETTING != "someCategory"){
												return;
											}
											var subgrid_table_id = subgrid_id+"_t";
											var pager_id = "p_"+subgrid_table_id;
											$("#"+subgrid_id).html("<table id='"+subgrid_table_id+"'></table><div id='"+pager_id+"'></div>");
											$("#"+subgrid_table_id).jqGrid({
														url:"querySomeCategory.action?PRO_ID="+ret.PRO_ID,
														datatype: "json",
														width:200,
														colNames: ["分类名称","分类编码"],
														colModel: [ {name:"CNAME", index:"CNAME", width:"90"},
																	{name:"CID", index:"CID", align:"center",hidden:true}],
													   	rowNum:5,
													   	pager: pager_id,
													    height: '100%',
													    jsonReader:{root:"dataset", 
															page:"pager.curPageNo", 
															total:"pager.totalPages", 
															records:"pager.totalCount", 
															repeatitems:false
														}, 
														prmNames:{rows:"pager.pageSize", 
																  page:"pager.curPageNo", 
																  sort:"pager.orderBy", 
																  order:"pager.order"
														}, 
														postData:{
															'pager.totalCount':function(){return getTotalCount($("#"+subgrid_table_id));}
														}
											});
										},
										subGridRowColapsed: function(subgrid_id, row_id) {
											var subgrid_table_id = subgrid_id+"_t";
											$("#"+subgrid_table_id).remove();
										}
	});
}

function setUpdateGridTimeout(){
  var gridName = 'queryUpdateGridTable';
  var rownum = $("#"+gridName).jqGrid("getGridParam", "rowNum");
  if(rownum===null){
    return;
  }
  var flag = false;
  for (var i = 1; i <= rownum; i++) {
      var ret = $("#"+gridName).jqGrid("getRowData", i);
      if (ret.TRADE_STATE == 'Q'||ret.TRADE_STATE == 'R'||ret.TRADE_STATE == '0'||ret.TRADE_STATE == '1') {
        flag = true;
        break;
      }
  }
  if(flag){
    setTimeout("updateGridTrigger()",10000);
  }
}

function updateGridTrigger(){
  $("#queryUpdateGridTable").trigger("reloadGrid");
}

function reUpdateButton(cellvalue, options, rowObject){
	gid = options.gid;
	var buttonInfo = "";
	if(rowObject.VALID_FLAG == "1" && (rowObject.TRADE_STATE == "2" || rowObject.TRADE_STATE == "5")){
		buttonInfo = "<input type='button' class='pbtn40' value='重新更换' onclick = 'reExecute(this," +rowObject.BATCH_ID + ")'>";
	}
	
	return buttonInfo;
}



